package com.hly.musicbackground.model.movies.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hly.musicbackground.Utils.JsoupBooksUtil;
import com.hly.musicbackground.Utils.RequestContextUtils;
import com.hly.musicbackground.dao.MoviesDao;
import com.hly.musicbackground.domain.Movies;
import com.hly.musicbackground.domain.MoviesEpisodes;
import com.hly.musicbackground.domain.MoviesLink;
import com.hly.musicbackground.model.movies.VO.MoviesDetailVO;
import com.hly.musicbackground.model.movies.VO.MoviesListVO;
import com.hly.musicbackground.model.movies.service.MoviesEpisodesService;
import com.hly.musicbackground.model.movies.service.MoviesLinkService;
import com.hly.musicbackground.model.movies.service.MoviesService;
import com.hly.musicbackground.result.Code;
import lombok.RequiredArgsConstructor;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;


@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class MoviesServiceImpl extends ServiceImpl<MoviesDao, Movies> implements MoviesService {

    private final MoviesEpisodesService moviesEpisodesService;
    private final MoviesLinkService moviesLinkService;

    private final MoviesTencentVideo moviesTencentVideo;
    private final MoviesIQIYI moviesIQIYI;
    private final MoviesBILIBILI moviesBILIBILI;

    @Override
    public MoviesListVO searchListDom(String key) {
        MoviesListVO moviesListVO = new MoviesListVO();
        Document TXSPDoc = JsoupBooksUtil.getAllDoc("https://v.qq.com/x/search/?q=" + key, "v.qq.com");
//        Document IQIYIDoc = JsoupBooksUtil.getAllDoc("https://so.iqiyi.com/so/q_" + key + "?source=history&sr=6872321729738576&ssrt=20230311152750729&ssra=1a1e8f4c4c768f47f332d201d75fce87", "so.iqiyi.com");
//        Document BIBLIBIBLIDoc = JsoupBooksUtil.getAllDoc("https://search.bilibili.com/all?keyword=" + key, "search.bilibili.com");
//        System.out.println(IQIYIDoc);
        try {
            moviesListVO.getList().addAll(moviesTencentVideo.searchDomToObject(TXSPDoc));
//            moviesListVO.getList().addAll(moviesIQIYI.searchDomToObject(IQIYIDoc));
//            moviesListVO.getList().addAll(moviesBILIBILI.searchDomToObject(BIBLIBIBLIDoc));
        }catch (Exception e){
            Code.BUSINESS_ERR("解析影视异常");
        }



        moviesListVO.setTotal(moviesListVO.getList().size());
        List<Movies> collect = moviesListVO.getList().stream().map(n -> {
            Movies one = lambdaQuery().eq(Movies::getImage, n.getImage())
                    .eq(Movies::getMoviesName, n.getMoviesName())
                    .one();
            if (one == null) {
                save(n);
                return n;
            }
            return one;
        }).collect(Collectors.toList());

        moviesListVO.setList(collect);

        return moviesListVO;
    }

    @Override
    public MoviesDetailVO getDetail(int id, int uid) {
        Movies byId = getById(id);
        if (byId == null){
            Code.BUSINESS_ERR("暂无该影视");
        }

        byId.setBrowse(byId.getBrowse() + 1);

        MoviesLink link = moviesLinkService.getAutoMoviesLink(uid, id);

        List<MoviesEpisodes> list = moviesEpisodesService.getMoviesEpisodesList(byId);

        updateById(byId);

        return MoviesDetailVO.builder()
                .moviesLink(link)
                .movies(byId)
                .moviesEpisodes(list).build();
    }

    @Override
    public List<Movies> getMoviesLinkList(int limit, int page, int type, int uid) {
        List<MoviesLink> links = moviesLinkService.getMoviesLinkList(limit, page, type, uid);
        List<Integer> ids = links.stream().map(n -> n.getMoviesId()).collect(Collectors.toList());
        return listByIds(ids);
    }

}
